package com.just.bz_back.delayqueue;

import com.just.bz_back.service.RedisService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;
import java.util.UUID;


/**
 * 延迟队列，
 * 5小时自动修改维修单星级
 */
@Service
public class DelayQueue {
    private final static int ADD_TIME= 1000*60*60;
    private final static String KEY= "SCORE_ZSET";
    @Resource
    private RedisService redisService;
    /**
     * 生产者,生成评星报单
     */
    public void productionDelayMessage() {
        String require_id= UUID.randomUUID().toString();
        //插入数据库订单表
        //设置Zset集合
        redisService.zAdd(KEY,require_id,new Date().getTime());

    }

    //消费者，取订单
    public  void consumerDelayMessage() {

        /*while (true) {*/
            Set<Object> sets= redisService.rangeByScore(KEY,new Date().getTime(),new Date().getTime()+ADD_TIME);
        System.out.println(sets.toString());
        for (Object o:sets) {
            System.out.println(o);
            //修改数据库状态
            //移除key

        }
            //int dec_time = new Date().getTime()-;
            System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ":redis消费了一个任务：消费的订单OrderId为");
        //}
    }


  /*   class DelayMessage implements Runnable{
        @Override
        public void run() {
            try {
               // cdl.await();
                DelayQueue.consumerDelayMessage();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }*/
}
